package com.shujia.day15;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Code09DealThread {
    public static final Lock lock1 = new ReentrantLock();
    public static final Lock lock2 = new ReentrantLock();


    public static void main(String[] args) {
        MyDealThread thread1 = new MyDealThread("线程1", true);
        MyDealThread thread2 = new MyDealThread("线程2", false);

        thread1.start();
        thread2.start();

    }
}


class MyDealThread extends Thread {

    boolean flag = true;

    public MyDealThread(String name, boolean flag) {
        super(name);
        this.flag = flag;
    }

    @Override
    public void run() {
        if (this.flag) {
            synchronized (Code09DealThread.lock1) {
                System.out.println("1 this is lock1 synchronized flag:" + flag);
                synchronized (Code09DealThread.lock2) {
                    System.out.println("1 this is lock2 synchronized flag:" + flag);
                }
            }
        }
        if (!this.flag) {
            synchronized (Code09DealThread.lock2) {
                System.out.println("2 this is lock2 synchronized flag:" + flag);
                synchronized (Code09DealThread.lock1) {
                    System.out.println("2 this is lock2 synchronized flag:" + flag);
                }
            }
        }

    }
}
